<?php

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/10/13 15:19
 * Desc: 专题
 */
class Model_Api_V2_Special
{
    // 模块名称数组
    private $module_name_arr = null;

    // 主表数组
    private $main_table_arr = null;

    /**
     * 获取优惠券
     * @param $channel_id
     * @return array
     */
    public function get_coupon($channel_id)
    {
        $models = $this->get_module_name();

        $list = DB::select('c.id', 'c.name', 'c.typeid', 'c.type', 'c.samount', 'c.amount', 'c.memeber_grades',
            'c.modules')
            ->from(array('zt_channel_coupon', 'z'))
            ->join(array('coupon', 'c'), 'left')
            ->on('z.couponid', '=', DB::expr('sline_c.id'))
            ->where('z.channelid', '=', $channel_id)
            ->and_where('c.kindid', '=', 1)
            ->and_where('c.isopen', '=', 1)
            ->and_where('c.isdel', '=', 0)
            ->and_where(DB::expr(" AND (sline_c.isnever=0 OR (sline_c.isnever=1 and sline_c.endtime > " . time() . "))"))
            ->execute()
            ->as_array();

        if (!empty($list)) {
            foreach ($list as &$item) {
                $model_name = array();
                if ($item['typeid'] == 1) {
                    $types = explode(',', $item['modules']);
                    foreach ($types as $type) {
                        if (isset($models[$type])) {
                            $model_name[] = $models[$type];
                        }
                    }
                } elseif ($item['typeid'] == 0) {
                    $model_name[] = '所有产品';
                }
                $item['modelName'] = implode(',', $model_name);
            }
        }
        return $list;
    }

    /**
     * 获取产品
     * @param $channel_id
     * @return array
     */
    public function get_product($channel_id)
    {
        $models = $this->get_main_table();

        $list = DB::select('typeid', 'productid')
            ->from('zt_channel_product')
            ->where('channelid', '=', $channel_id)
            ->order_by('displayorder', 'asc')
            ->execute()
            ->as_array();

        $data       = array();
        $able_model = array(1, 2, 3, 5, 13, 21, 101, 114, 115, 118, 121);
        foreach ($list as $item) {
            if (!in_array($item['typeid'], $able_model) || !isset($models[$item['typeid']])) {
                continue;
            }

            $table_name = $models[$item['typeid']];
            if($item['typeid']!=13){
                $field = ['title', 'id', 'litpic', 'price', 'satisfyscore', 'bookcount'];
            }else{
                $field = ['title', 'id', 'litpic', 'price', 'satisfyscore', 'bookcount', 'starttime', 'endtime'];
            }
            $product    = DB::select(DB::expr(implode(',',$field)))
                ->from($table_name)
                ->where('id', '=', $item['productid'])
                ->and_where('ishidden', '=', 0)
                ->execute()
                ->current();
            if ($product) {
                $product['litpic']    = Model_Api_V2_System::uniapp_img($product['litpic']);
                $product['typeid']    = $item['typeid'];
                if($product['starttime']>time() && $product['endtime']>time()){
                    $product['tuan_status'] = 1;
                    $product['countdown'] = intval(bcmul(bcsub($product['starttime'], time(), 0),1000));
                }elseif($product['starttime']<=time() && $product['endtime']>time()){
                    $product['tuan_status'] = 2;
                    $product['countdown'] = intval(bcmul(bcsub($product['endtime'], time(), 0),1000));
                }else{
                    $product['tuan_status'] = 3;
                    $product['countdown'] = 0;
                }
                $product['starttime'] = date('Y-m-d H:i:s', $product['starttime']);
                $product['endtime']   = date('Y-m-d H:i:s', $product['endtime']);

                $data[]               = $product;
            }
        }
        return $data;
    }

    /**
     * 获取文章
     * @param $channel_id
     * @return array
     */
    public function get_article($channel_id)
    {
        $models = $this->get_main_table();

        $list = DB::select('typeid', 'productid')
            ->from('zt_channel_product')
            ->where('channelid', '=', $channel_id)
            ->order_by('displayorder', 'asc')
            ->execute()
            ->as_array();

        $data = array();
        foreach ($list as $item) {
            if (!isset($models[$item['typeid']])) {
                continue;
            }
            $table_name = $models[$item['typeid']];
            $da         = DB::select('title', 'id', 'litpic')
                ->from($table_name)
                ->where('id', '=', $item['productid'])
                ->and_where('ishidden', '=', 0)
                ->execute()
                ->current();
            if ($da) {
                $da['litpic'] = Model_Api_V2_System::uniapp_img($da['litpic']);
                $da['typeid'] = $item['typeid'];
                $data[]       = $da;
            }
        }
        return $data;
    }

    /**
     * 设置module_name
     * @return array
     */
    private function get_module_name()
    {
        if ($this->module_name_arr == null) {
            $models                = DB::select('id', 'modulename')
                ->from('model')
                ->where('is_couponable', '=', 1)
                ->where('isopen', '=', 1)
                ->execute()
                ->as_array('id', 'modulename');
            $this->module_name_arr = $models;
        }

        return $this->module_name_arr;
    }

    /**
     * 设置main_table
     * @return array
     */
    private function get_main_table()
    {
        if ($this->main_table_arr == null) {
            $models               = DB::select('id', 'maintable')
                ->from('model')
                ->where('isopen', '=', 1)
                ->execute()
                ->as_array('id', 'maintable');
            $this->main_table_arr = $models;
        }

        return $this->main_table_arr;
    }
}